package a_o2odata_deal.utils.zhibo_zf

import org.apache.spark.sql.SQLContext
import org.apache.spark.{SparkConf, SparkContext}

/**
  * @ Auther: o2o-rd-0008
  * @ Date:   2020/1/14 12:30
  * @ Param:  ${PARAM}
  * @ Description: 
  */
object handle_shop {

  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setAppName(s"${this.getClass.getSimpleName}")
    conf.set("spark.debug.maxToStringFields", "500")
    //conf.setMaster("local[*]")
    conf.set("es.nodes", "192.168.1.157")
    conf.set("es.port", "9200")
    conf.set("cluster.name", "O2OElastic")
    conf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
    conf.set("spark.sql.caseSensitive", "true")
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)
    sc.setLogLevel("WARN")
    sc.hadoopConfiguration.set("fs.s3a.access.key", "GAO7EO9FWKPJ8WFCQDME")
    sc.hadoopConfiguration.set("fs.s3a.secret.key", "LZ0xaHBSYKHaJ9ECDbX9f7zin79UZkXfGoNapRPL")
    //sc.hadoopConfiguration.set("fs.s3a.endpoint", "https://obs.cn-north-1.myhuaweicloud.com")

    for (months <- 1 to 1){
      //加载天猫 淘宝数据
      val tb_data = sqlContext.read.orc(s"s3a://o2o-dataproces-group/panzonghao/taobao/2020/${months}/zhibo_finally")
        .selectExpr("platformId","shopId","is_showLive","timeStamp")
      val tm_data = sqlContext.read.orc(s"s3a://o2o-dataproces-group/panzonghao/tmall/2020/${months}/zhibo_finally")
        .selectExpr("platformId","shopId","is_showLive","timeStamp")

      tb_data.createOrReplaceTempView("tb_data")
      tm_data.createOrReplaceTempView("tm_data")

      sqlContext.udf.register("handle_is_showLive",handle_is_showLive _)
      //只有一个店铺只有一个直播商品就算直播店铺，否则就不是直播店铺
      val tb_result = sqlContext.sql(
        s"""
           |select
           |platformId,
           |timeStamp,
           |shopId,
           |handle_is_showLive(is_showLive_collect) as is_liveShop
           |from
           |(select
           |platformId,
           |timeStamp,
           |shopId,
           |collect_list(is_showLive) as is_showLive_collect
           |from tb_data
           |group by platformId,timeStamp,shopId)
         """.stripMargin)

      val tm_result = sqlContext.sql(
        s"""
           |select
           |platformId,
           |timeStamp,
           |shopId,
           |handle_is_showLive(is_showLive_collect) as is_liveShop
           |from
           |(select
           |platformId,
           |timeStamp,
           |shopId,
           |collect_list(is_showLive) as is_showLive_collect
           |from tm_data
           |group by platformId,timeStamp,shopId)
         """.stripMargin)

      tb_result.selectExpr("platformId","timeStamp","shopId","is_liveShop").union(
        tm_result.selectExpr("platformId","timeStamp","shopId","is_liveShop")
      ).repartition(12).write.orc(s"s3a://o2o-dataproces-group/panzonghao/zhibo_zf/2020/${months}/is_liveShop/")
    }


  }


  import scala.util.control.Breaks.{break, breakable}
  def handle_is_showLive(is_showLive_collect:Seq[String]):String={
    var b = "false"
    breakable {
      for (i <- 0 to is_showLive_collect.length-1){
        if (is_showLive_collect(i)=="true"){
          b = "true"
          break
        }
      }
    }

    b
  }


}
